﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Helpers;
using Meraz.Core.Model;
using Meraz.Core.Repositories;
using Ninject;
using System.Text.RegularExpressions;

namespace Meraz.Core.Commands
{
    public interface ICreateTwitterUserCommand 
    {
        ITwitterUserRepository TwitterUserRepository { get; set; }
        TwitterUser Execute(long userID, string username);
    }
    public class CreateTwitterUserCommand : BaseCommand, ICreateTwitterUserCommand
    {
        [Inject]
        public INetworkHelper NetworkHelper { get; set; }

        [Inject]
        public ITwitterUserRepository TwitterUserRepository { get; set; }

        public bool IsValidUsername(string username, out string message)
        {
            message = "";
            if (string.IsNullOrWhiteSpace(username))
                message = "Username is empty!";
            else if (username.Length > 32)
                message = "Username is too long!";
            else if (Regex.IsMatch(username,@"[^\da-z_]+",RegexOptions.IgnoreCase))
                message = "Username contains bad characters";
            return message.Length == 0;
        }

        public TwitterUser Execute(long userID, string username)
        {
            long id = 0;
            TwitterUser user;
            if (userID == 0)
            {
                string message;
                if (!IsValidUsername(username, out message))
                    throw new ApplicationException(message);
                user = TwitterUserRepository.GetByUsername(username);
                if (user == null)
                {
                    id = GetUserIDFromApi(username);
                    if (id == 0)
                        throw new ApplicationException("Failed to find a twitter user with that username");
                    user = new TwitterUser();
                    TwitterUserRepository.Add(user);
                    user.ID = id;
                    user.Name = username;
                    Commit();
                }
                else
                    return user;
            }
            else
            {
                user = TwitterUserRepository.GetById(userID);
                if (user == null)
                {
                    user = new TwitterUser();
                    TwitterUserRepository.Add(user);
                    user.ID = userID;
                    user.Name = username;
                    Commit();
                }
            }
            return user;
        }

        public long GetUserIDFromApi(string username)
        {
            string pattern = @"http://twitter.com/statuses/user_timeline/(?<ID>\d+).rss";
            string url = string.Format("http://twitter.com/{0}", username);
            string html = NetworkHelper.Download(url);
            Match match = Regex.Match(html, pattern);
            long result;
            if (match.Success)
            {
                if (long.TryParse(match.Groups["ID"].Value, out result))
                    return result;
            }
            return 0;
        }
    }
}
